<?php
/* SECURITY (don't touch) */
defined('_INCL')||(header('HTTP/1.1 403 Forbidden')&die('403.14 - Access denied.'));
/**************************/

?>

<form id="QRinput">
<style scoped>
	#QRinput label.Count { text-align:right; }
	
</style>

	<h1>Opret nye medlemskort</h1>
	<p style="color:#800;"><b>OBS!</b> Kør højest 1000 enheder ad gangen, da denne server ikke kan trække mere. Når vi engang får egen server, kan vi formentlig producere 100.000+ per kørsel :)</p>
	
	<hr/>
	
	<label class='Half1'>Udbyg produktions-serie:
	<? 
		if( $ProdSeries['Existing']['Info']['Series'] === 0 ){
			echo "<select name='ProdSerieOld' disabled>";
			echo "<option value='' disabled>Ingen eksisterende</option>";
		} else {
			echo "<select name='ProdSerieOld' onchange='Serievalg(\"Old\")'>";
			echo "<option value='' disabled>Vælg ...</option>";
			
			$TempExisting = $ProdSeries['Existing']; unset($TempExisting['Info']);
			foreach($TempExisting as $Alphas => $Unit){
				if( $Unit !== end($TempExisting) ){//Not last Unit
					echo "<option value='$Alphas' data-start='".$Unit['Info']['Max']."'>Serie $Alphas (".$Unit['Info']['Total']." enheder)</option>";
				} else {
					echo "<option value='$Alphas' data-start='".$Unit['Info']['Max']."' selected>Serie $Alphas (".$Unit['Info']['Total']." enheder)</option>";
				}
			}
		}
		echo "</select>".PHP_EOL;
	?>
	</label>
	<label  class='Half2'>Ny produktions-serie:
	<? 
		$Output = '';
		foreach($ProdSeries['Free'] as $Alphas => $Empty){
			$Output .= "<option value='$Alphas'>Serie $Alphas</option>";
		}
		if( empty($Output) ){
			echo "<select name='ProdSerieNew' disabled>";
			echo "<option value='' disabled>Ingen ledige</option>";
		} else {
			echo "<select name='ProdSerieNew' onchange='Serievalg(\"New\")'>";
			echo "<option value='' disabled>Vælg ...</option>";
			echo $Output;
		} unset($Output);
		echo "</select>".PHP_EOL;
	?>
	</label>

	<fieldset>
		<legend>Medlemsnumre:</legend>
		<label class='Third1'>Fra og med:
			<input type="number" name="NumStart" value="1" min="1" max="999901" step="50" onchange="Medlemsnummer(0)" disabled/>
		</label>
		<label class='Third2'>Til og med:
			<input type="number" name="NumEnd" value="50" min="50" max="999950" step="50" onchange="Medlemsnummer(1)"/>
		</label>
		<label class='Third3 Count'><br/>= <b id="NumTotal">??</b> enheder</label>
	</fieldset>
	<input type="submit" value="Kør serie">

</form>
<script>
	$( window ).ready(function() {
		Serievalg('Old');
	});

	$('form#QRinput').submit(function(e){
		e.preventDefault();
		$('div#Wait').toggle();
		Content = new Array();
		Content[0] = 'Action=Make';
		Content[1] = 'ProdSerieOld=' + $("form#QRinput select[name|='ProdSerieOld']").val();
		Content[2] = 'ProdSerieNew=' + $("form#QRinput select[name|='ProdSerieNew']").val();
		Content[3] = 'NumStart=' + $("form#QRinput input[name|='NumStart']").val();
		Content[4] = 'NumEnd=' + $("form#QRinput input[name|='NumEnd']").val();
		location.replace('QRcreate.main.php?'+Obfuscate(Content));
	});
	
	function Serievalg(type){
		SelectOld = $("form#QRinput select[name|='ProdSerieOld']");
		SelectNew = $("form#QRinput select[name|='ProdSerieNew']");
		ItemNumA = $("form#QRinput input[name|='NumStart']");
		ItemNumB = $("form#QRinput input[name|='NumEnd']");
		
		ValOld = SelectOld.children('option:selected').data('start') + 1;
		
		if( type === 'Old' ){
			if( !isNaN(ValOld) ){
				SelectNew.val('');
				ItemNumA.attr('min',ValOld);
				ItemNumB.attr('min',ValOld+49);
				ItemNumA.val(ValOld);
			}
			Medlemsnummer(0);
		} else if( type === 'New' ){
			SelectOld.val('');
			ItemNumA.attr('min',1);
			ItemNumB.attr('min',50);
			ItemNumA.val('1');
			ItemNumB.val('50');
		}
		
	}
	
	function Medlemsnummer(type){
		ItemA = $("form#QRinput input[name|='NumStart']");
		ItemB = $("form#QRinput input[name|='NumEnd']");
		ItemC = $("form#QRinput b#NumTotal");

		Step = Number(ItemA.attr('step'));
		ValA = Number(ItemA.val());
		ValB = Number(ItemB.val());

		if( ValA<1 || ValA>999900 ){
			ValA = 1;
			ItemA.val(ValA);
		}
		if( ValB<50 || ValB>999950 ){
			ValB = ValA+Step-1;
			ItemB.val(ValB);
		}

		if( type === 0 && ValA >= ValB ){
			ValB = ValA+Step-1;
			ItemB.val(ValB);
		} else if( type === 1 && ValB <= ValA ){
			ValA = ValB-Step+1;
			ItemA.val(ValA);
		}		
	
		ValC = ZeroPad(ValB-ValA +1,6);
		ItemC.text(ValC);
	}
</script>
